﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Google.GData.Apps;

namespace UserAccountsManager
{
    public partial class OknoGesla : Form
    {
        private class LocalUser
        {
            public String Username { get; set; }
            public String Password { get; set; }
            public LocalUser(string username, string password)
            {
                Username = username;
                Password = password;
            }
        }

        List<LocalUser> users = new List<LocalUser>();
        public AppsService service;
        public String domena;
        public String organizacijskaEnota;

        public OknoGesla()
        {
            InitializeComponent();
            status.Text = "Pripravljen";
        }

        private void buttonCommit_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListViewItem item in listViewUporabniki.Items) users.Add(new LocalUser(item.SubItems[2].Text, item.SubItems[3].Text));
                saveFileDialog1.FileName = organizacijskaEnota + "-" + domena + ".csv";
                if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Stream stream = saveFileDialog1.OpenFile();
                    //Stream stream = File.Open(saveFileDialog1.FileName, FileMode.Truncate);
                    Encoding enc = Encoding.UTF8;
                    TextWriter writer = new StreamWriter(stream, enc);
                    if (saveFileDialog1.FilterIndex == 1)
                    {
                        writer.Write("first name,last name,email address,password\r\n");
                        foreach (ListViewItem item in listViewUporabniki.Items)
                        {
                            writer.Write(item.SubItems[0].Text.Split(' ')[0] + "," + item.SubItems[0].Text.Split(' ')[1] + "," + item.SubItems[1].Text + "," + item.SubItems[3].Text + "\r\n");
                        }
                    }
                    else
                    {
                        writer.Write("Ime Priimek Email Geslo\r\n");
                        foreach (ListViewItem item in listViewUporabniki.Items)
                        {
                            writer.Write(item.SubItems[0].Text.Split(' ')[0] + " " + item.SubItems[0].Text.Split(' ')[1] + " " + item.SubItems[1].Text + " " + item.SubItems[3].Text + "\r\n");
                        }
                    }
                    writer.Close();
                }
                backgroundWorkerUserUpdater.RunWorkerAsync();
            }
            catch (AppsException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void backgroundWorkerUserUpdater_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                String timeleft = "";
                for (int i = 0; i < users.Count; i++)
                {
                    LocalUser usr = users[i];
                    DateTime time1 = DateTime.Now;
                    status.Text = "Posodabljam uporabnika " + usr.Username + ". " + timeleft;
                    UserEntry user = service.RetrieveUser(usr.Username);
                    user.Login.Password = usr.Password;
                    if (checkBox1.Checked) user.Login.ChangePasswordAtNextLogin = true;
                    service.UpdateUser(user);
                    DateTime time2 = DateTime.Now;
                    timeleft = "Do konca še približno " + ((time2 - time1).Seconds * (users.Count - i)).ToString() + " sekund.";
                }
            }
            catch (AppsException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void backgroundWorkerUserUpdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
